package cdu.javaendwork.Controller.Shopping;

import cdu.javaendwork.model.sqlModel.Order;
import cdu.javaendwork.service.Impl.Shopping.OrderServiceImpl;
import cdu.javaendwork.service.Interface.Shopping.OrderService;
import cdu.javaendwork.util.ProNumber.ProNumBerHelper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;

@WebServlet("/buying")
public class BuyingController extends HttpServlet {
    OrderService orderService = new OrderServiceImpl();
    ProNumBerHelper proNumBerHelper = new ProNumBerHelper();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //拿到要支付的购物车id
        String cid = req.getParameter("craftid");
        int craftid = Integer.parseInt(cid);

        //查询到该购物车
        Order craft = orderService.findById(craftid);
        //计算订单号
        int pro =  (int)(Math.random()*9000+1000);
        String ref = pro + proNumBerHelper.getProNumBerForDate(craft.getDate().toString());
        //生成订单
        orderService.update(craftid,craft.getUserid(),2,new Timestamp(new Date().getTime()),craft.getPrice(),ref);
        //重定向回超市
        req.getRequestDispatcher("market").forward(req,resp);
    }
}
